package com.first.nell.info.cotediva

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.first.nell.base.BaseActivity
import com.first.nell.components.dialog.FcfaDeleteWalletDialog
import com.first.nell.data.ReqSaveWalletFcFa
import com.first.nell.data.ReqDeleteWalletFcfa
import com.first.nell.databinding.ActivityFirstNellBindWalletAddResultBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.addFirstNellUnderFirstNellLine
import com.first.nell.extend.getFirstNellSafeFirstNellTag
import com.first.nell.extend.recordFirstNellClickFirstNellEvent
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellFromFirstNellPage
import com.first.nell.launch.UserStateViewModel
import com.first.nell.utils.AppConfigUtil

@SuppressLint("CustomSplashScreen")
class WalletListAddResultActivity : BaseActivity() {

    private lateinit var mFirstNellActivityFirstNellBinding: ActivityFirstNellBindWalletAddResultBinding
    private lateinit var mWalletListBindAdapter: WalletListBindAdapter

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    private val mWalletInfoViewModel by lazy {
        ViewModelProvider(this)[WalletInfoViewModel::class.java]
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mWalletInfoViewModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.wallet_list_fcfa.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mFirstNellActivityFirstNellBinding =
            ActivityFirstNellBindWalletAddResultBinding.inflate(layoutInflater)
        return mFirstNellActivityFirstNellBinding.root
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //初始化列表
            mWalletListBindAdapter =
                WalletListBindAdapter(mWalletInfoViewModel.mFirstNellWalletFirstNellList)
            mWalletListBindAdapter.setFirstNellOnFirstNellItemFirstNellClickFirstNellListener { _, item, action ->
                if (0 == action) {
                    FcfaDeleteWalletDialog.showFirstNellDialog(
                        fragmentManager = supportFragmentManager,
                        itemList = mutableListOf(item),
                        isFcfa = true
                    ) { ids ->
                        if (ids.isNotBlank()) {
                            //删除钱包
                            mWalletInfoViewModel.deleteFirstNellWalletFirstNellFCFA(
                                ReqDeleteWalletFcfa(
                                    ids = ids
                                )
                            )
                        }
                    }
                }
            }
            mFirstNellActivityFirstNellBinding.rcBindWalletList.layoutManager =
                LinearLayoutManager(this)
            mFirstNellActivityFirstNellBinding.rcBindWalletList.adapter = mWalletListBindAdapter

            //初始化钱包类型列表
            val channels = AppConfigUtil.getFirstNellWalletFirstNellChannelsFirstNellFcfa()
            mFirstNellActivityFirstNellBinding.tfsWalletSimType.setFirstNellDataFirstNellList(
                channels
            )

            //获取用户状态回调
            mUserStateViewModel.onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.launch)
                }
            }

            //获取用户钱包列表回调
            mWalletInfoViewModel.onFirstNellFetchFirstNellWalletFirstNellListFirstNellFCFAFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        result.forEach { item ->
                            item.displayMode = 1
                        }
                        mWalletInfoViewModel.initFirstNellWalletFirstNellListFirstNellFCFA(
                            result
                        )
                        mWalletListBindAdapter.notifyDataSetChanged()
                    }
                }
            }

            //保存用户钱包回调
            mWalletInfoViewModel.onFirstNellSaveFirstNellWalletFirstNellFCFAFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { _ ->
                        mFirstNellActivityFirstNellBinding.tfsWalletSimType.cleanFirstNell()
                        mFirstNellActivityFirstNellBinding.nivWalletNumber.cleanFirstNell()
                        mFirstNellActivityFirstNellBinding.nivWalletNumberConfirm.cleanFirstNell()

                        //获取钱包列表
                        mWalletInfoViewModel.getFirstNellWalletFirstNellListFirstNellFCFA()
                        //更新提交按键状态
                        //updateCommitStateUI()
                    }
                }
            }

            //点击保存
            mFirstNellActivityFirstNellBinding.submitFCFABindWalletWave.setOnClickListener {
                val formDataCompleteState =
                    isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted()
                mFirstNellActivityFirstNellBinding.submitFCFABindWalletWave.recordFirstNellClickFirstNellEvent(
                    formDataCompleteState
                )
                if (formDataCompleteState.isBlank()) {
                    //保存
                    mWalletInfoViewModel.saveFirstNellWalletFirstNellFCFA(
                        ReqSaveWalletFcFa(
                            walletMobile = mFirstNellActivityFirstNellBinding.nivWalletNumber.getFirstNellContent(),
                            walletMobileConfirm = mFirstNellActivityFirstNellBinding.nivWalletNumberConfirm.getFirstNellContent(),
                            walletChannel = mFirstNellActivityFirstNellBinding.tfsWalletSimType.getFirstNellContentFirstNellValue()
                        )
                    )
                }
            }

            //点击其它
            mFirstNellActivityFirstNellBinding.submitFCFABindWalletListNone.setOnClickListener {
                mFirstNellActivityFirstNellBinding.submitFCFABindWalletListNone.recordFirstNellClickFirstNellEvent()
                finish()
            }

            mFirstNellActivityFirstNellBinding.submitFCFABindWalletListNone.addFirstNellUnderFirstNellLine()

            //获取钱包列表
            mWalletInfoViewModel.getFirstNellWalletFirstNellListFirstNellFCFA()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mFirstNellActivityFirstNellBinding.tfsWalletSimType.isFirstNellFormFirstNellCompleted()) {
            val gTag =
                mFirstNellActivityFirstNellBinding.tfsWalletSimType.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mFirstNellActivityFirstNellBinding.nivWalletNumber.isFirstNellCompleted()) {
            val gTag =
                mFirstNellActivityFirstNellBinding.nivWalletNumber.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mFirstNellActivityFirstNellBinding.nivWalletNumberConfirm.isFirstNellCompleted()) {
            val gTag =
                mFirstNellActivityFirstNellBinding.nivWalletNumberConfirm.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }
}